<template>
  <div class="user-management">
    <div class="page-header">
      <h2>用户管理</h2>
      <el-button type="primary" @click="refreshUsers">刷新</el-button>
    </div>
    
    <!-- 用户列表 -->
    <el-card shadow="never" class="user-list-card">
      <el-table 
        :data="userList" 
        style="width: 100%" 
        v-loading="loading"
        border
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" width="150" />
        <el-table-column prop="userType" label="用户类型" width="120">
          <template #default="scope">
            <el-tag :type="getUserTypeTag(scope.row.userType)">
              {{ getUserTypeText(scope.row.userType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button 
              type="danger" 
              size="small" 
              @click="handleDeleteUser(scope.row)"
              :disabled="scope.row.userType === 'admin'"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllUsers, deleteUser } from '@/api/admin'

const loading = ref(false)
const userList = ref([])

const getUserTypeTag = (userType) => {
  switch (userType) {
    case 'admin': return 'danger'
    case 'company': return 'success'
    case 'worker': return 'primary'
    default: return 'info'
  }
}

const getUserTypeText = (userType) => {
  switch (userType) {
    case 'admin': return '管理员'
    case 'company': return '企业用户'
    case 'worker': return '零工用户'
    case 'customer': return '客服人员'
    default: return '未知'
  }
}

const formatDate = (date) => {
  if (!date) return '-'
  const d = new Date(date)
  return d.toLocaleString()
}

const loadUsers = async () => {
  loading.value = true
  try {
    const response = await getAllUsers()
    if (response && response.data) {
      userList.value = response.data
    } else {
      userList.value = []
    }
  } catch (error) {
    console.error('获取用户列表失败:', error)
    ElMessage.error('获取用户列表失败')
  } finally {
    loading.value = false
  }
}

const refreshUsers = () => {
  loadUsers()
}

const handleDeleteUser = (user) => {
  // 不允许删除管理员
  if (user.userType === 'admin') {
    ElMessage.warning('不允许删除管理员账号')
    return
  }
  
  ElMessageBox.confirm(
    `确定要删除用户 "${user.username}" 吗？此操作不可逆！`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(async () => {
      try {
        const response = await deleteUser(user.id)
        if (response && response.data) {
          ElMessage.success('删除用户成功')
          loadUsers() // 重新加载用户列表
        } else {
          ElMessage.error('删除用户失败')
        }
      } catch (error) {
        console.error('删除用户失败:', error)
        ElMessage.error(error.message || '删除用户失败')
      }
    })
    .catch(() => {
      // 用户取消删除操作
    })
}

onMounted(() => {
  loadUsers()
})
</script>

<style scoped>
.user-management {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
}

.user-list-card {
  margin-bottom: 20px;
}
</style> 